.TH std::basic_istream::peek 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_istream::peek \- std::basic_istream::peek

.SH Synopsis
   int_type peek();

   Behaves as UnformattedInputFunction. After constructing and testing the sentry
   object, reads the next character from the input stream without extracting it.

.SH Parameters

   \fI(none)\fP

.SH Return value

   If good() == true, returns the next character as obtained by rdbuf()->sgetc().

   Otherwise, returns Traits::eof().

.SH Exceptions


   failure if an error occurred (the error state flag is not goodbit) and exceptions()
   is set to throw for that state.

   If an internal operation throws an exception, it is caught and badbit is set. If
   exceptions() is set for badbit, the exception is rethrown.

.SH Example


// Run this code

 #include <iostream>
 #include <sstream>

 int main()
 {
     std::istringstream s1("Hello, world.");
     char c1 = s1.peek();
     char c2 = s1.get();
     std::cout << "Peeked: " << c1 << " got: " << c2 << '\\n';
 }

.SH Output:

 Peeked: H got: H

.SH See also

   sgetc reads one character from the input sequence without advancing the sequence
         \fI(public member function of std::basic_streambuf<CharT,Traits>)\fP
   get   extracts characters
         \fI(public member function)\fP
   unget unextracts a character
         \fI(public member function)\fP
